﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.BLL;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.DataAccessLayers;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF010 - Nhap hang tu don dat hang
    /// </summary>
    /// <author>viet sang</author>
    public partial class MF010 : frmManager
    {
        private DT_SanPhamBLL _business;
        private int _idDonHang;

        public MF010()
        {
            InitializeComponent();

            pnlChiTiet.Enabled = true;
            _business = new DT_SanPhamBLL();
            cboDonDatHang.SelectedValueChanged += new EventHandler(cboDonDatHang_SelectedValueChanged);

        }

        protected override void LoadData()
        {
            tsbLuu.Enabled = true;
            tsbLuu.Text = "Lưu";

            LoadDonDatHang();

            base.LoadData();
        }

        private void LoadDonDatHang()
        {
            //load combobox
            List<DT_DonDatHang> data = Cache.Context.DT_DonDatHang.Where(s => s.IDTrangThaiDonHang == (int?)StructsEnums.EnumTrangThaiDonHang.DangDatHang).ToList();
            if (data != null && data.Count>0)
            {
                cboDonDatHang.DataSource = data;
                cboDonDatHang.DisplayMember = "MaDonDatHang";
                cboDonDatHang.ValueMember = "IDDonDatHang";
                cboDonDatHang.Enabled = true;
                return;
            }
            cboDonDatHang.Enabled = false;
            tsbLuu.Enabled = false;
        }

        protected override void Search()
        {
            base.Search();
            if (cboDonDatHang.SelectedValue != null)
            {               
                if (int.TryParse(cboDonDatHang.SelectedValue.ToString(), out _idDonHang))
                {
                    ////List<View_DatHangNhaCungCap> data = Cache.Context.View_DatHangNhaCungCap.Where(s => s.IDDonDatHang == _idDonHang).ToList();
                    //List<View_DatHangNhaCungCap> data=null;
                    //Cache.CreateNewContext();
                    //IQueryable<View_DatHangNhaCungCap> list = Cache.Context.View_DatHangNhaCungCap.Where(s => s.IDDonDatHang == _idDonHang);

                    //if(list!=null)
                    //{
                    //    data = list.ToList(); //Cache.Context.View_DatHangNhaCungCap.Where(s => s.IDDonDatHang == _idDonHang).ToList();
                    //}

                    //IQueryable<View_DatHangNhaCungCap> data = from dh in Cache.Context.View_DatHangNhaCungCap
                    //                                          where dh.IDDonDatHang == _idDonHang
                    //                                          select dh;

                    var lstData = (from dh in Cache.Context.DT_DonDatHang
                                join ctdh in Cache.Context.DT_ChiTietDonHang on dh.IDDonDatHang equals ctdh.IDDonDatHang
                                join sp in Cache.Context.DT_SanPham on ctdh.IDSanPhan equals sp.IDSanPham
                                where dh.IDDonDatHang == _idDonHang
                                select new
                                {
                                    IDSanPham = sp.IDSanPham
                                    ,
                                    MaSanPham = sp.MaSanPham
                                    ,
                                    TenSanPham = sp.TenSanPham
                                    ,
                                    SoLuongDat = ctdh.SoLuong
                                });

                    List<DonDatHang> data = lstData.ToList().ConvertAll<DonDatHang>(s => new DonDatHang
                    { 
                        IDSanPham = s.IDSanPham,
                        MaSanPham = s.MaSanPham,
                        TenSanPham =s.TenSanPham,
                        SoLuongDat =s.SoLuongDat??0
                    });

                    //List<DonDatHang> dsDonDatHang = data.ConvertAll<DonDatHang>(s =>  {  s.MaSanPham,s.TenSanPham,s.SoLuongDat });
                    //QuanLyBanHangEntities ctx = new QuanLyBanHangEntities();
                    //List<View_DatHangNhaCungCap> data = ctx.View_DatHangNhaCungCap.ToList();

                    //data = data.Where(s => s.IDDonDatHang == _idDonHang).ToList();

                    _bindingSource.DataSource = data;
                    grid2.DataSource = data;

                    //tsbLuu.Enabled = data.Count > 0 ? true : false;

                }
            }
        }

        protected override int UpdateData()
        {
            try
            {

                int kq=-1;
                List<DonDatHang> data = _bindingSource.DataSource as List<DonDatHang>;

                if (data.Count > 0)
                {
                    DT_SanPham sanPhan = null;
                    DT_DonDatHang donHang = null;
                    foreach (DonDatHang item in data)
                    {
                        //sanPhan = Cache.Context.DT_SanPham.Where(s => s.IDSanPham == item.IDSanPham).FirstOrDefault();
                        sanPhan = _business.Context.DT_SanPham.Where(s => s.IDSanPham == item.IDSanPham).FirstOrDefault();
                        if (sanPhan != null)
                        {
                            sanPhan.SoLuongTonKho += item.SoLuongDat;
                        }
                    }

                    //donHang = Cache.Context.DT_DonDatHang.Where(s => s.IDDonDatHang == _idDonHang).FirstOrDefault();
                    donHang = _business.Context.DT_DonDatHang.Where(s => s.IDDonDatHang == _idDonHang).FirstOrDefault();
                    donHang.IDTrangThaiDonHang = (int)StructsEnums.EnumTrangThaiDonHang.DaNhapHang;
                    donHang.DaThanhToan = donHang.TongTien;

                    kq = _business.Context.SaveChanges();//Cache.Context.SaveChanges();//_business.Context.SaveChanges();
                }
                
                if (kq > 0)
                {
                    UICommon.ShowMessgeString("Nhập thành công", "Thông báo");
                    //reload data
                    LoadDonDatHang();
                    return kq;
                }

                else
                {
                    UICommon.ShowMessgeString("Nhập hàng thất bại", "Thông báo");
                    return -1;
                }
            }
            catch {
                UICommon.ShowMessgeString("Nhập hàng thất bại", "Thông báo");
                return -1;
            };            
        }

        private void cboDonDatHang_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboDonDatHang.SelectedIndex != -1)
            {
                Search();
            }
        }

        public class DonDatHang
        {
            //public DonDatHang()
            //{

            //}

            //public DonDatHang(string msp, string tsp, int soluong)            
            //{ 
            //}
            public int IDSanPham { get; set; }
            public string MaSanPham { get; set; }
            public string TenSanPham { get; set; }
            public int SoLuongDat { get; set; }
        }

    }
}
